package com.ellen.mac.chatdemo.viewholder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.avos.avoscloud.im.v2.messages.AVIMAudioMessage;
import com.ellen.mac.chatdemo.R;
import com.ellen.mac.chatdemo.utils.LocalCacheUtils;
import com.ellen.mac.chatdemo.utils.LogUtils;
import com.ellen.mac.chatdemo.utils.PathUtils;
import com.ellen.mac.chatdemo.view.PlayButton;

/**
 * Created by wli on 15/9/17.
 * 聊天页面中的语音 item 对应的 holder
 */
public class ChatItemAudioHolder extends ChatItemHolder {

    protected PlayButton playButton;
    protected TextView durationView;
    protected LinearLayout item;
    protected SeekBar seekbar;

    private static double itemMaxWidth = 0;
    private static int itemMinWidth = 200;

    private Context context;
    public ChatItemAudioHolder(Context context, ViewGroup root, boolean isLeft) {
        super(context, root, isLeft);
        this.context = context;
    }

    @Override
    public void initView() {
        super.initView();
        if (isLeft) {
            conventLayout.addView(View.inflate(getContext(), R.layout.play_voice_item, null));
//      conventLayout.addView(View.inflate(getContext(), R.layout.chat_item_left_audio_layout, null));
        } else {
            conventLayout.addView(View.inflate(getContext(), R.layout.chat_item_right_audio_layout, null));
        }

        item = itemView.findViewById(R.id.item);
        playButton = itemView.findViewById(R.id.chat_item_audio_play_btn);
        durationView = itemView.findViewById(R.id.chat_item_audio_duration_view);
        seekbar = itemView.findViewById(R.id.seekbar_item);

        if (itemMaxWidth <= 0) {
            itemMaxWidth = itemView.getResources().getDisplayMetrics().widthPixels * 0.6;
        }
    }

    @Override
    public void bindData(Object o) {
        super.bindData(o);
        if (o instanceof AVIMAudioMessage) {
            AVIMAudioMessage audioMessage = (AVIMAudioMessage) o;
            durationView.setText(String.format("%.0f\"", audioMessage.getDuration()));

            double duration = audioMessage.getDuration();
            int width = getWidthInPixels(duration);
            if (width > 0) {
//        item.setLayoutParams(new LinearLayout.LayoutParams(width,100));
                playButton.setWidth(width);
                if (isLeft) {
                    seekbar.setMax((int)audioMessage.getDuration());
                    LogUtils.i(seekbar.getMax()+"========max");
                    playButton.initSeekBar(seekbar,context);
                }
            }

            String localFilePath = audioMessage.getLocalFilePath();
            if (!TextUtils.isEmpty(localFilePath)) {
                playButton.setPath(localFilePath);
            } else {
                String path = PathUtils.getAudioCachePath(getContext(), audioMessage.getMessageId());
                if (path != null) {
                    playButton.setPath(path);
                    LocalCacheUtils.downloadFileAsync(audioMessage.getFileUrl(), path);
                } else {
                    LogUtils.i( "path == null ===========================");
                }
            }
        }
    }

    private int getWidthInPixels(double duration) {
        if (itemMaxWidth <= 0) {
            return 0;
        }
        double unitWidth = itemMaxWidth / 100;
        if (duration < 2) {
            return itemMinWidth;
        } else if (duration < 10) {
            return itemMinWidth + (int) (unitWidth * 5 * duration);
        } else {
            return itemMinWidth + (int) (unitWidth * 50) + (int) (unitWidth * (duration - 10));
        }
    }
}